package cl.alanis.bd;
import cl.alanis.util.AlanisLog;
import java.sql.*;
import org.apache.log4j.Logger;





/**
 * Esta case es la que realmente implementa la conexion a la BD.
 * 
 * @author Yerko Covacevich
 */
public class ConectaBD {

	private Connection conn;
	
	Logger logger=AlanisLog.obtenerLogger(ConectaBD.class);
	private String host, user, pass;
        
        private static ConectaBD conexion;
	/**
	 * Constructor
	 * 
	 * @param drv
	 *            El nombre del Driver
	 * @param hos
	 *            El nombre del host
	 * @param usr
	 *            El nombre del usuario
	 * @param pas
	 *            El password
	 */
	private ConectaBD() throws ClassNotFoundException, InstantiationException, IllegalAccessException, SQLException {
		
		host = "jdbc:odbc:AlanisBD";//hos;
		user = "";//usr;
		pass = "";//pas;

		conn=getConn();


	}

    public Connection getConn() throws ClassNotFoundException, InstantiationException, IllegalAccessException, SQLException {
        Class.forName("sun.jdbc.odbc.JdbcOdbcDriver").newInstance();
        conn = DriverManager.getConnection(host, user, pass);
        conn.setAutoCommit(false);
        logger.debug("Conexion realizada exitosamente a "+host);
        return conn;
        
    }

    public static ConectaBD getInstance() throws ClassNotFoundException, InstantiationException, IllegalAccessException, SQLException{
        if(conexion==null)
            conexion=new ConectaBD();
        return conexion;
    }
    public void commit() throws SQLException {
        conn.commit();

    }

    public PreparedStatement preparedStatement(String sql) throws SQLException  {
            
            return conn.prepareStatement(sql);
        
       
    }
    public Statement statement() throws SQLException  {
        
            return conn.createStatement();
        
       
    }
    public void close() {
        try {
            conn.close();
        } catch (SQLException ex) {
            logger.error("Error: " + ex.getMessage());
        }
    }

    public void rollback() throws SQLException {
        conn.rollback();
    }
	
}